package cn.lysj.framework.api.controller;

import cn.lysj.framework.api.base.ApiContent;
import cn.lysj.framework.common.utils.web.ParamUtil;
import cn.lysj.framework.common.utils.web.Resp;
import cn.lysj.framework.common.utils.websocket.IncidentType;
import cn.lysj.framework.common.utils.websocket.WebSocketServerToUser;
import cn.lysj.framework.customer.domain.Customer;
import cn.lysj.framework.customer.service.CustomerService;
import cn.lysj.framework.store.domain.CustEatTable;
import cn.lysj.framework.store.service.CustEatTableService;
import io.swagger.annotations.Api;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: ZhangWenJian
 * @Create: 2018/11/14--14:34
 * @Description:
 */
@RestController
@RequestMapping("/api/v1/user")
@Api(value = "ApiCustEatTableController", tags = "菜单模块接口")
public class ApiCustEatTableController extends ApiContent {


    @GetMapping("/show_cust_eat_table")
    public Resp showCustEatTable(String eatTableId){
        Long time = System.currentTimeMillis() - 5*60*1000;
        Date fiveMinBefore = new Date(time);
        return new Resp(custEatTableService.getRepository().findShowCustEatTableVoByEatTableId(eatTableId,fiveMinBefore));
    }

    @PostMapping("/msg_redio")
    public Resp msgRedio(Integer incidentType,String date,String custEatTableId,String customerId){
        if(ParamUtil.isBlank(customerId)){
            return new Resp(Resp.NO_LOGIN,"用户信息有误!");
        }
        Customer customer = customerService.findOne(customerId);
        if(customer == null){
            return new Resp(Resp.NO_LOGIN,"用户信息有误!");
        }
        if(ParamUtil.isBlank(custEatTableId)){
            return new Resp(Resp.PARAM_ERROR,"未查询到拼单信息!");
        }
        CustEatTable custEatTable = custEatTableService.getRepository().findOne(custEatTableId);
        if(custEatTable == null){
            return new Resp(Resp.PARAM_ERROR,"未查询到拼单信息!");
        }
        custEatTable.setUpdateTime(new Date());
        custEatTableService.save(custEatTable);
        String[] objectIds = custEatTable.getInitiatorId().split("-");
        for (int i = objectIds.length-1; i>=0 ;i--){
            if(objectIds[i].equals(customerId)){
                objectIds = (String[]) ArrayUtils.remove(objectIds,i);
                break;
            }
        }
        Integer informNum = WebSocketServerToUser.sendDataToMorePeopleArray(incidentType,date,objectIds,custEatTableId);
        return new Resp("共通知"+informNum+"人");
    }

    @PostMapping("/set_up_cust_eat_table")
    public Resp setUpCustEatTable(String customerId,String eatTableId,String name){
        if(ParamUtil.isBlank(customerId)){
            return new Resp(Resp.NO_LOGIN,"用户信息有误!");
        }
        Customer customer = customerService.findOne(customerId);
        if(customer == null){
            return new Resp(Resp.NO_LOGIN,"用户信息有误!");
        }
        if(ParamUtil.isBlank(eatTableId)){
            return new Resp(Resp.PARAM_ERROR,"桌号信息有误");
        }
        CustEatTable custEatTable = new CustEatTable();
        custEatTable.setName(name);
        custEatTable.setEatTableId(eatTableId);
        custEatTable.setInitiatorId(customerId);
        custEatTable.setInitiatorName(customer.getNickName());
        custEatTable.setParticipateIds(customerId);
        custEatTable.setStatus(1);
        custEatTable = custEatTableService.getRepository().save(custEatTable);
        return new Resp(custEatTable);
    }
    /**
     * @author ZhangWenJian
     * @date 2019/2/20 9:58
     * @title addCustEatTable
     * @param customerId
     * @param custEatTableId
     * @return cn.lysj.framework.common.utils.web.Resp
     * @description 有新的用户加入拼桌操作
     */
    @PostMapping("/add_cust_eat_table")
    public Resp addCustEatTable(String customerId,String custEatTableId){
        if(ParamUtil.isBlank(customerId)){
            return new Resp(Resp.NO_LOGIN,"用户信息有误!");
        }
        Customer customer = customerService.findOne(customerId);
        if(customer == null){
            return new Resp(Resp.NO_LOGIN,"用户信息有误!");
        }
        if(ParamUtil.isBlank(custEatTableId)){
            return new Resp(Resp.PARAM_ERROR,"未查询到拼单信息!");
        }
        CustEatTable custEatTable = custEatTableService.getRepository().findOne(custEatTableId);
        if(custEatTable == null){
            return new Resp(Resp.PARAM_ERROR,"未查询到拼单信息!");
        }
        //使用时间来判断一张桌子是否失效
        Date nowDate = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);
        calendar.add(Calendar.SECOND, -20);
        nowDate = calendar.getTime();
        //如果桌子处于已经状态
        if(custEatTable.getStatus() == 2){
            if(custEatTable.getUpdateTime().getTime() > nowDate.getTime()){
                custEatTable.setStatus(1);
                custEatTableService.save(custEatTable);
            }
            else{
                return new Resp(Resp.PARAM_ERROR,"该拼单已经结束!");
            }
        }
        //桌子可以继续添加人 这个人没有在用户列表中
        if(custEatTable.getInitiatorId().indexOf(customerId) == -1){
            if(ParamUtil.isBlank(custEatTable.getInitiatorId())){
                custEatTable.setInitiatorId(customerId);
            }
            else{
                custEatTable.setInitiatorId(custEatTable.getInitiatorId()+"-"+customerId);
            }
            custEatTableService.getRepository().save(custEatTable);
        }
        //向其他人请求数据
        WebSocketServerToUser.getFoodListRequest(custEatTable,customerId,custEatTableId);
        return new Resp("加入拼桌成功!");
    }

    @PostMapping("/send_food_order_to_new_friend")
    public Resp sendFoodOrderToNewFriend(String data,String customerId,String custEatTableId){
        if(ParamUtil.isBlank(custEatTableId)){
            return new Resp(Resp.PARAM_ERROR,"未查询到拼单信息!");
        }
        CustEatTable custEatTable = custEatTableService.getRepository().findOne(custEatTableId);
        if(custEatTable == null){
            return new Resp(Resp.PARAM_ERROR,"未查询到拼单信息!");
        }
        custEatTable.setUpdateTime(new Date());
        custEatTableService.save(custEatTable);
        if ( WebSocketServerToUser.sendDataToOnePeople(IncidentType.GET_FOOD_LIST,data,customerId,custEatTableId)){
            return new Resp("发送购物列表成功!");
        }
        else{
            return new Resp(Resp.PARAM_ERROR,"发送失败,该对象不在线");
        }
    }

    @PostMapping("/food_redio")
    public Resp foodRedio(String customerId,String newFoodInfo,String allFoodInfo,String custEatTableId,String barrage){
        if(ParamUtil.isBlank(customerId)){
            return new Resp(Resp.NO_LOGIN,"用户信息有误!");
        }
        Customer customer = customerService.findOne(customerId);
        if(customer == null){
            return new Resp(Resp.NO_LOGIN,"用户信息有误!");
        }
        if(ParamUtil.isBlank(custEatTableId)){
            return new Resp(Resp.PARAM_ERROR,"未查询到拼单信息!");
        }
        CustEatTable custEatTable = custEatTableService.getRepository().findOne(custEatTableId);
        if(custEatTable == null){
            return new Resp(Resp.PARAM_ERROR,"未查询到拼单信息!");
        }
        custEatTable.setUpdateTime(new Date());
        custEatTableService.save(custEatTable);
        String[] objectIds = custEatTable.getInitiatorId().split("-");
        for (int i = objectIds.length-1; i>=0 ;i--){
            if(objectIds[i].equals(customerId)){
                objectIds = (String[]) ArrayUtils.remove(objectIds,i);
                break;
            }
        }
        custEatTable.setFoodOrderInfo(allFoodInfo);
        custEatTableService.getRepository().save(custEatTable);
        Map<String,Object> map = new HashMap<>();
        map.put("newFoodInfo",newFoodInfo);
        map.put("barrage",barrage);
        Integer informNum = WebSocketServerToUser.sendDataToMorePeopleArray(IncidentType.GET_NEW_FOOD_INFO,map,objectIds,custEatTableId);
        return new Resp("共通知"+informNum+"人");
    }



}
